//
//  HELBindPhoneVC.m
//  NursingAssociation
//
//  Created by wsj2015 on 2017/3/1.
//  Copyright © 2017年 wsj2015. All rights reserved.
//

#import "HELBindPhoneVC.h"
#import "BFPaperButton.h"
#import "NSString+Utilities.h"
#import "NSObject+CountDown.h"
@interface HELBindPhoneVC ()

@property (weak, nonatomic) IBOutlet UITextField *codeTf;

@property (weak, nonatomic) IBOutlet UITextField *statusTF;
@property (weak, nonatomic) IBOutlet UITextField *phoneTf;


@property (weak, nonatomic) IBOutlet BFPaperButton *codeBtn;

@property (weak, nonatomic) IBOutlet BFPaperButton *bindBtn;

@property (nonatomic,  assign) NSInteger currentStatu;


@end

@implementation HELBindPhoneVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = kTableView_bg;
    self.title  =@"绑定手机";
    
    UIBarButtonItem *left =   [[UIBarButtonItem  alloc] initWithImage:[UIImage imageNamed:@"nav_back"] style:UIBarButtonItemStyleDone target:self action:@selector(close)];
 
    self.navigationItem.leftBarButtonItem = left;
    
    [self setViewByBindStatu:NO];
   
  
    
}

- (void)setViewByBindStatu:(BOOL)isAnimate{
    
    
    HELUser *user =[HELAppManager userInfo];
    if ([user.phoneBindStatus integerValue]== 0) {
        [self.bindBtn setTitle:@"绑定" forState:UIControlStateNormal];
        self.statusTF.text = @"未绑定";
        self.currentStatu =  0;
        self.phoneTf.userInteractionEnabled = YES;
        self.title  =@"绑定手机";
        self.phoneTf.text = user.mobileNumber?user.mobileNumber:@"";
    }else{
        self.statusTF.text = @"已绑定";
        self.phoneTf.text = user.mobileNumber;
        self.phoneTf.userInteractionEnabled  =NO;
        [self.bindBtn setTitle:@"取消绑定" forState:UIControlStateNormal];
        self.currentStatu = 1;
        self.title  =@"解绑手机";
    }
    self.codeTf.text = @"";
    if (isAnimate) {
        
        
        [UIView animateWithDuration:1
                              delay:0
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             //你想绕哪个轴哪个轴就为 1，其中的参数（角度， x, y, z）
                             self.bindBtn.layer.transform=CATransform3DMakeRotation(M_PI, 0, 1, 0);
                             self.statusTF.layer.affineTransform = CGAffineTransformScale(self.statusTF.layer.affineTransform, 3, 3);
                             
                             
                         }
                         completion:^(BOOL finished) {
                             [UIView animateWithDuration:1
                                                   delay:0
                                                 options:UIViewAnimationOptionCurveEaseInOut
                                              animations:^{
                                                  //你想绕哪个轴哪个轴就为 1，其中的参数（角度， x, y, z）
                                                  self.bindBtn.layer.transform=CATransform3DIdentity;
                                                  self.statusTF.layer.affineTransform = CGAffineTransformIdentity;
                                              }
                                              completion:^(BOOL finished) {
                                                  
                                                  
                                              }];
                             
                         }];
    }
    
  
}

- (void)close{
   
    [self dismissViewControllerAnimated:YES completion:^{
        [self.codeBtn cancelTimer];
    }];
}
                             

- (IBAction)codeAction:(id)sender {
    if ([self.phoneTf.text isEmpty]) {
        cwqAlertMessage(@"手机号不能空");
        return ;
    }
    if (![self.phoneTf.text isMobile]) {
        cwqAlertMessage(@"请输入正确的手机号");
        return;
    }
    
    
    weakSelf_define;
    
    
    NSString *url = nil;
    if (self.currentStatu == 0) {
        url =[NSString stringWithFormat:@"%@/%@",helVerifyCode_bindPhone,self.phoneTf.text];
    }else{
        url = [NSString stringWithFormat:@"%@",helverifyCode_unbindPhone];
    }
    
    [[HELNetworkManager sharedMg] GET:url parameters:nil success:^(id responseObject) {
       
        cwqHudSuccess(@"验证码发送成功!");
        
        //倒计时
        if ([responseObject[@"data"] containsString:@"验证码发送成功"]) {
            
            [weakSelf.codeBtn countDownTime:60 countDownBlock:^(NSUInteger timer) {
                
                [weakSelf.codeBtn setBackgroundColor:[UIColor lightGrayColor]];
                [weakSelf.codeBtn setTitle:[NSString stringWithFormat:@"%zd 秒", timer] forState:UIControlStateNormal];
                weakSelf.codeBtn.enabled = NO;
               
            } outTimeBlock:^{
                [weakSelf resetCountBtn];
                
            }];
            
        }else {
            cwqHudRequestError(responseObject[@"exception"]);
        }
        weakSelf.codeBtn.enabled = YES;
    } failure:^(NSString *err) {
      
        cwqAlertMessage(@"发送失败");
        weakSelf.codeBtn.enabled = YES;
       
        
    }];

    
    
}


- (void)resetCountBtn{

    
        [self.codeBtn cancelTimer];
        [self.codeBtn setBackgroundColor:HexRGB_WSJ(246, 80, 110)];
        [self.codeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.codeBtn setTitle:@"验证码" forState:UIControlStateNormal];
        self.codeBtn.enabled = YES;
  
}

- (IBAction)bindAction:(id)sender {
  
    
  
    if (self.currentStatu == 1) {
        [self cancelBind];
        return;
    }
    
    
    if ([self.phoneTf.text isEmpty]) {
        cwqAlertMessage(@"手机号不能空");
        return ;
    }
    if (![self.phoneTf.text isMobile]) {
        cwqAlertMessage(@"请输入正确的手机号");
        return;
    }
    if ([self.codeTf.text isEmpty]) {
        cwqAlertMessage(@"验证码不能为空");
        return;
    }
    [self resetCountBtn];;
    cwqHud(self.view);
    weakSelf_define;
    [[HELNetworkManager sharedMg] GET:[NSString stringWithFormat:@"%@/%@",helBindPhont,self.codeTf.text] parameters:nil success:^(id responseObject) {
          [hud hide:YES];
        if ([responseObject[@"err"] isKindOfClass:[NSString class]]) {
            cwqAlertMessage(responseObject[@"err"]);
        }else{
            cwqHudSuccess(@"成功绑定!");
            weakSelf.statusTF.text = @"已绑定";
            
            [HELAppManager updateBindPhoneStatu:1 withPhone:weakSelf.phoneTf.text];
            [weakSelf setViewByBindStatu:YES];
        }
      
        
    } failure:^(NSString *err) {
          [hud hide:YES];
    }];
    
    
}
- (void)cancelBind{
    if ([self.phoneTf.text isEmpty]) {
        cwqAlertMessage(@"手机号不能空");
        return ;
    }
    if (![self.phoneTf.text isMobile]) {
        cwqAlertMessage(@"请输入正确的手机号");
        return;
    }
    if ([self.codeTf.text isEmpty]) {
        cwqAlertMessage(@"验证码不能为空");
        return;
    }
    [self resetCountBtn];
    cwqHud(self.view);
    weakSelf_define;
    [[HELNetworkManager sharedMg] GET:[NSString stringWithFormat:@"%@/%@",helUnBindPhont,self.codeTf.text] parameters:nil success:^(id responseObject) {
        [hud hide:YES];
        cwqHudSuccess(@"成功取消绑定!");
        [HELAppManager updateBindPhoneStatu:0 withPhone:nil];
        [weakSelf setViewByBindStatu:YES];
        
    } failure:^(NSString *err) {
        [hud hide:YES];
    }];
    

}




@end
